package com.rx.core.base;

import com.netflix.hystrix.exception.HystrixRuntimeException;
import com.rx.core.enm.RESULT_CODE;
import com.rx.core.enm.RX_LOG_MODE;
import com.rx.core.util.EmptyChecker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 控制器
 */
@Slf4j
@ControllerAdvice
public class RxBaseController {

    /**
     * 统一绑定处理
     * @param dataBinder
     */
    @InitBinder
    public void initBinder(WebDataBinder dataBinder){
        SimpleDateFormat dataFormat = new SimpleDateFormat(RxBaseConstant.DATE_FORMAT_DATETIME);
        dataFormat.setLenient(false);
        dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dataFormat,true));
    }

    private ModelAndView setResult(ModelAndView mv,String code,String desc){
        mv.addObject("resultCode",code);
        mv.addObject("resultMessage",desc);
        return mv;
    }
    /**
     * 异常统一处理
     * @param request
     * @param e
     * @return
     */
    @ExceptionHandler
    @ResponseBody
    public ModelAndView exp(HttpServletRequest request, Exception e){
        ModelAndView mv = new ModelAndView(new MappingJackson2JsonView());
        mv.addObject("success",false);
        if(e instanceof RxBaseException){
            RxBaseException rxBaseException = (RxBaseException)e;
            log.error(RX_LOG_MODE.ERROR.getFormat(rxBaseException));
            return setResult(mv,rxBaseException.getResultCode(),rxBaseException.getResultMessage());
        }else if (e instanceof NullPointerException){
            log.error(RX_LOG_MODE.ERROR.getFormat("空异常"));
            return setResult(mv,RESULT_CODE.EXCP.getCode(),"空异常");
        }else if (e instanceof HystrixRuntimeException){
            HystrixRuntimeException hystrixRuntimeException = (HystrixRuntimeException)e;
            log.error(RX_LOG_MODE.ERROR.getFormat("熔断异常"));
            if(EmptyChecker.notEmpty(hystrixRuntimeException.getFallbackException())){
                if(EmptyChecker.notEmpty(hystrixRuntimeException.getFallbackException().getCause())){
                    if(hystrixRuntimeException.getFallbackException().getCause().getCause() instanceof RxBaseException){
                        RxBaseException rxBaseException = (RxBaseException)hystrixRuntimeException.getFallbackException().getCause().getCause();
                        return setResult(mv,rxBaseException.getResultCode(),rxBaseException.getResultMessage());
                    }
                }
            }

        }
        return setResult(mv,RESULT_CODE.EXCP.getCode(),e.getMessage());
    }
}
